#include "ObjLoader.h"
#include <fstream>
#include <iostream>
//#define scale 1
using namespace std;

ObjLoader::ObjLoader(wstring filename)
{
	FILE *fp;

	if ((fp = _wfopen(filename.c_str(), L"r")) == NULL) {
		cerr << "file cannot be read.\n";
		return;
	}

	char dummy, buf[512];
	float coord_in[3];
	int v_id[3];
	while (!feof(fp))
	{
		fgets(buf, 512, fp);
		if (feof(fp))
			break;
		if (buf[0] ^ 'v' && buf[0] ^ 'f')
			continue;
		else if (!(buf[0] ^ 'v'))
		{
			sscanf(buf, "%c%f%f%f", &dummy, &coord_in[0], &coord_in[1], &coord_in[2]);
#ifdef scale
			coord_in[0]/=scale;
			coord_in[1]/=scale;
			coord_in[2]/=scale;
#endif
			vector<GLfloat> Point(coord_in, coord_in+3);
			vSets.push_back(Point);
		}
		else
		{
			sscanf(buf, "%c%d%d%d", &dummy, &v_id[0], &v_id[1], &v_id[2]);
			v_id[0]--;
			v_id[1]--;
			v_id[2]--;
			vector<GLint>vIndexSets(v_id, v_id+3);
			fSets.push_back(vIndexSets);
		}
	}
	
	fclose(fp);
}

const vector<vector<GLint>>& ObjLoader::getFaceSets() const
{
	return fSets;
}

const vector<vector<GLfloat>>& ObjLoader::getVertexSets() const
{
	return vSets;
}
